import myrequest from '../../utils/myrequest'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        value: "",
        username: "", //用户名
        telphone: "", //手机号
        detailaddress: "", //详细地址
        plate: "",
        index: '',
        state:null,
    },

    async save_address() {
        //地址保存业务
        let {
            plate,
            value,
            username,
            telphone,
            detailaddress
        } = this.data
        //把写好的地址保存到本地和服务器
        // wx.setStorageSync('address', {})
        let {
            data
        } = await myrequest({
            url: '/saveaddress',
            method: "POST",
            data: {
                plate,
                value,
                username,
                telphone,
                detailaddress
            }
        })
        console.log(data);
    },
    // 保存并使用按钮
    save_fn() {
        // this.fn()
        // console.log("按钮", event);
        
        //保存用户地址信息
        let {
            plate,
            value,
            username,
            telphone,
            detailaddress,
            index,
            state,
        } = this.data
        //填完地址之后，返回到订单页，从从订单页才去其他页面
        if (value.length == 0 || username.length == 0 || telphone.length == 0 || detailaddress.length == 0 || plate.length == 0) {
            return wx.showToast({
                title: "信息不全",
                icon: "error"
            })
        }
        //接口请求
        this.save_address()
        console.log("11111111", value)
        wx.navigateTo({
            url: '/pages/add/add?user=' + username + "&value=" + value + "&plate=" + plate + "&phone=" + telphone + "&address=" + detailaddress + "&index=" + index + "&state=" + state
        })
    },
    select_fn(event) {
        console.log(event);
        //选择城市的函数
        let value = event.detail.value.join(" ")
        this.setData({
            value
        })
    },
    getplate(event) {
        //获取门牌号
        this.setData({
            plate: event.detail.value
        })
        // console.log(this.data.plate)
    },
    getuser(event) {
        //获取用户名
        this.setData({
            username: event.detail.value
        })
    },
    getphone(event) {
        //获取手机号
        this.setData({
            telphone: event.detail.value
        })
    },
    getaddress(event) {
        console.log(event);
        //获取详细地址
        this.setData({
            detailaddress: event.detail.value
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log("aaa", options)
        let index = options.index
        this.setData({
            index
        })
        console.log(index)
         //url是没有携带用户信息
         let newaddress = wx.getStorageSync('newaddress')
         newaddress = newaddress.split(' ')
         console.log("分割后的", newaddress)
         console.log(newaddress);
         this.setData({
             value: "",
             detailaddress: "",
             username: "",
             telphone: "",
             plate: ""
         })
        // if (options.user) {
        //     //从订单页跳过来带地址
        //     console.log(options)
        //     let value = options.value
        //     let username = options.user
        //     let telphone = options.phone
        //     let detailaddress = options.address
        //     let plate = options.plate
        //     let index = options.index
        //     let state = options.state
        //     this.setData({
        //         value,
        //         username,
        //         telphone,
        //         detailaddress,
        //         plate,
        //         index,
        //         state,
        //     })
        // } else {
           
        // }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})